//
//  ShareAccount.m
//  NeteaseLottery
//
//  Created by wangbo on 11-7-13.
//  Copyright 2011 netease. All rights reserved.
//

#import "ShareAccount.h"
#import "ShareKit.h"

#define kSinaOAuthConsumerKey				@"2286779076"
#define kSinaOAuthConsumerSecret			@"c5623b3a11e550d1f4cd43099d5134bc"
#define kNTESOAuthConsumerKey				@"HujY1Y4ium4kWeBe"
#define kNTESOAuthConsumerSecret			@"RxTO9jwBcWQ2i3rzLvGdkiTJcFGekhe5"
#define kQQOAuthComsumeKey                  @"801082015"
#define kQQOAuthComsumerSecret              @"bf8487998f6492280960d9c54738846a"


#define kConfigKey            @"ShareAccount"
#define kAuthorizeDataKey     @"authorizeData"
#define kAccountSreenNameKey  @"screenName"

@implementation ShareAccount

@dynamic consumerKey;
@dynamic consumerSecret;
@dynamic authorizeData;
@dynamic screenName;

+(ShareAccount*)accountWithType:(NSString*)shareType {
    ShareAccount *account =[[ShareAccount alloc] initWithType:shareType];
    return account;
}

-(id)initWithType:(NSString*)shareType {
    self =[super init];
    if (self) {
        _configDic = [[[NSUserDefaults standardUserDefaults] objectForKey:kConfigKey] retain];
        if (!_configDic) {
            _configDic = [[NSMutableDictionary dictionaryWithCapacity:4] retain];
        }
        _shareType =[shareType copy];
    
    }
    return self;
}

-(void)dealloc {
    [_configDic release];
    [_shareType release];
    [super release];
}

-(void)saveAccount {
    [[NSUserDefaults standardUserDefaults] setObject:_configDic forKey:kConfigKey];
}

-(NSString*)consumerKey {
    if ([_shareType isEqualToString:SHARE_SINAWEIBO]) {
        return kSinaOAuthConsumerKey;
    } else if ([_shareType isEqualToString:SHARE_NTESWEIBO]) {
        return kNTESOAuthConsumerKey;
    } else if ([_shareType isEqualToString:SHARE_QQWEIBO]) {
        return kQQOAuthComsumeKey;
    }
    return nil;
}

-(NSString*)consumerSecret {
    if ([_shareType isEqualToString:SHARE_SINAWEIBO]) {
        return kSinaOAuthConsumerSecret;
    } else if ([_shareType isEqualToString:SHARE_NTESWEIBO]) {
        return kNTESOAuthConsumerSecret;
    } else if ([_shareType isEqualToString:SHARE_QQWEIBO]) {
        return kQQOAuthComsumerSecret;
    }
    return nil;
}

-(NSString*)authorAccount {
    if ([_shareType isEqualToString:SHARE_SINAWEIBO]) {
        return @"2177698641";
    } else if ([_shareType isEqualToString:SHARE_NTESWEIBO]) {
        return @"4987422016900171153";
    } else if ([_shareType isEqualToString:SHARE_QQWEIBO]) {
        return @"68304297";
    }
    return nil;
}

-(NSString*)authorizeData{
    NSDictionary *dict =[_configDic objectForKey:_shareType];
    return [dict objectForKey:kAuthorizeDataKey];
}

-(void)setAuthorizeData:(NSString*)data{
    NSMutableDictionary *typeDict =[_configDic objectForKey:_shareType];  
    if (!typeDict) {
        typeDict =[NSMutableDictionary dictionaryWithCapacity:3];
        [_configDic setObject:typeDict forKey:_shareType];
    }
    [typeDict setObject:data forKey:kAuthorizeDataKey];
    
    [self saveAccount];
}

-(NSString*)screenName{
    NSDictionary *dict =[_configDic objectForKey:_shareType];
    if ([dict objectForKey:kAccountSreenNameKey]) {
        return [dict objectForKey:kAccountSreenNameKey];  
    } else {
        return @"";
    }
}

-(void)setScreenName:(NSString *)name{
    NSMutableDictionary *typeDict =[_configDic objectForKey:_shareType];  
    if (!typeDict) {
        typeDict =[NSMutableDictionary dictionaryWithCapacity:3];
        [_configDic setObject:typeDict forKey:_shareType];
    }
    if (name) {
        [typeDict setObject:name forKey:kAccountSreenNameKey];        
        [self saveAccount];
    }
}

@end
